package com.weaver.view;

import com.weaver.controller.Controller;
import com.weaver.model.Model;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;

/**
 * ControlPanel class - Displays the control panel
 */
public class ControlPanel extends JPanel {
    private Model model;
    private Controller controller;
    
    private JButton resetButton;
    private JButton newGameButton;
    private JCheckBox showErrorMessageCheckBox;
    private JCheckBox showPathCheckBox;
    private JCheckBox useRandomWordsCheckBox;
    
    /**
     * Constructor
     * @param model game model
     * @param controller game controller
     */
    public ControlPanel(Model model, Controller controller) {
        this.model = model;
        this.controller = controller;
        
        // Set layout
        setLayout(new GridLayout(7, 1, 5, 10));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        
        // Create show error message checkbox
        showErrorMessageCheckBox = new JCheckBox("Show Error");
        showErrorMessageCheckBox.setSelected(model.isShowErrorMessage());
        showErrorMessageCheckBox.setActionCommand("TOGGLE_ERROR_MESSAGE");
        showErrorMessageCheckBox.addActionListener(controller);
        
        // Create show path checkbox
        showPathCheckBox = new JCheckBox("Look");
        showPathCheckBox.setSelected(model.isShowPath());
        showPathCheckBox.setActionCommand("TOGGLE_SHOW_PATH");
        showPathCheckBox.addActionListener(controller);
        
        // Create random words checkbox
        useRandomWordsCheckBox = new JCheckBox("Random");
        useRandomWordsCheckBox.setSelected(model.isUseRandomWords());
        useRandomWordsCheckBox.setActionCommand("TOGGLE_RANDOM_WORDS");
        useRandomWordsCheckBox.addActionListener(controller);

        // Create reset button
        resetButton = new JButton("Restart"){
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Color bgColor = getModel().isPressed() ? Color.GRAY :
                        getModel().isRollover() ? Color.LIGHT_GRAY : new Color(86, 249, 75);
                g2.setColor(bgColor);
                g2.fillRoundRect(0, 0, getWidth(), getHeight(), 40, 40);
                super.paintComponent(g);
            }
        };
        resetButton.setActionCommand("RESTART");
        resetButton.addActionListener(controller);
        resetButton.setEnabled(false); // Initially disabled

        // Create new game button
        newGameButton = new JButton("Run"){
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                Color bgColor = getModel().isPressed() ? Color.GRAY :
                        getModel().isRollover() ? Color.LIGHT_GRAY : new Color(86, 249, 75);

                g2.setColor(bgColor);
                g2.fillRoundRect(0, 0, getWidth(), getHeight(), 40, 40);

                super.paintComponent(g);
            }
        };
        newGameButton.setActionCommand("RUN");
        newGameButton.addActionListener(controller);



        newGameButton.setContentAreaFilled(false);
        newGameButton.setBorderPainted(false);
        newGameButton.setForeground(Color.WHITE);

        resetButton.setContentAreaFilled(false);
        resetButton.setBorderPainted(false);
        resetButton.setForeground(Color.WHITE);

        // Add components
        add(new JSeparator());
        add(showPathCheckBox);
        add(useRandomWordsCheckBox);
        add(showErrorMessageCheckBox);
        add(newGameButton);
        add(resetButton);
    }
    
    /**
     * Update the control panel
     */
    public void updateControls() {
        // Enable reset button if there are attempts
        resetButton.setEnabled(model.getAttemptsCount() > 0);
        
        // Update checkbox states
        showErrorMessageCheckBox.setSelected(model.isShowErrorMessage());
        showPathCheckBox.setSelected(model.isShowPath());
        useRandomWordsCheckBox.setSelected(model.isUseRandomWords());
    }
}